package com.frcdi.qms.web.controller.masterdata;

import com.frcdi.qms.common.constant.UserConstants;
import com.frcdi.qms.common.core.controller.BaseController;
import com.frcdi.qms.common.core.domain.AjaxResult;
import com.frcdi.qms.common.core.page.TableDataInfo;
import com.frcdi.qms.common.utils.DateUtils;
import com.frcdi.qms.common.utils.StringUtils;
import com.frcdi.qms.common.utils.poi.ExcelUtil;
import com.frcdi.qms.masterdata.domain.Change;
import com.frcdi.qms.masterdata.service.IChangeService;
import com.frcdi.qms.masterdata.service.IFileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 变更申请Controller
 *
 * @author cell
 * @date 2023-09-25
 */
@Api(tags = "变更申请")
@RestController
@RequestMapping("/masterData/change")
public class ChangeController extends BaseController
{
    @Autowired
    private IChangeService changeService;

    @Autowired
    private IFileService fileService;

    /**
     * 查询变更申请列表
     */
    @ApiOperation("查询变更申请列表")
    @PreAuthorize("@ss.hasPermi('masterData:change:list')")
    @GetMapping("/list")
    public TableDataInfo list(Change change)
    {
        startPage();
        List<Change> list = changeService.selectChangeList(change);
        return getDataTable(list);
    }

    /**
     * 导出变更申请列表
     */
    @ApiOperation("导出变更申请列表")
    @PreAuthorize("@ss.hasPermi('masterData:change:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, Change change)
    {
        List<Change> list = changeService.selectChangeList(change);
        ExcelUtil<Change> util = new ExcelUtil<Change>(Change.class);
        util.exportExcel(response, list, "变更申请数据");
    }

    /**
     * 获取变更申请详细信息
     */
    @ApiOperation("获取变更申请详细信息")
    @PreAuthorize("@ss.hasPermi('masterData:change:query')")
    @GetMapping(value = "/{changeId}")
    public AjaxResult getInfo(@PathVariable("changeId") Long changeId)
    {
        Change change = changeService.selectChangeByChangeId(changeId);
        if (change != null) {
            change.setFileList(fileService.selectFilesListByChangeNO(change.getChangeNo()));
        }
        return success(change);
    }
    @ApiOperation("获取变更申请详细信息")
    @PreAuthorize("@ss.hasPermi('masterData:change:query')")
    @GetMapping(value = "/getChange/{changeNo}")
    public AjaxResult getInfoByChangeNo(@PathVariable("changeNo") String changeNo)
    {
        Change change = changeService.selectChangeByChangeNo(changeNo);
        if (change != null) {
            change.setFileList(fileService.selectFilesListByChangeNO(change.getChangeNo()));
        }
        return success(change);
    }

    /**
     * 新增变更申请
     */
    @Transactional
    @ApiOperation("新增变更申请")
    @PreAuthorize("@ss.hasPermi('masterData:change:add')")
    @PostMapping
    public AjaxResult add(@RequestBody Change change)
    {
        if (!changeService.checkChangeNoUnique(change.getChangeNo())){
            return error(change.getChangeNo() + "'失败，变更单编号已存在");
        }
        if (StringUtils.isNotEmpty(change.getSubmitBy())){
            change.setSubmitTime(DateUtils.getNowDate());
        }
        change.setCreateBy(getUsername());
        return success(changeService.insertChange(change));
    }

    /**
     * 修改变更申请
     */
    @ApiOperation("修改变更申请")
    @PreAuthorize("@ss.hasPermi('masterData:change:edit')")
    @PutMapping
    public AjaxResult edit(@RequestBody Change change)
    {
        change.setUpdateBy(getUsername());
        // 编辑时重置所有状态
        change.setAuditResult(UserConstants.AUDIT_NOT_AUDITED);
        change.setTaskStatus(UserConstants.TASK_NOT);
        return toAjax(changeService.updateChange(change));
    }

    /**
     * 修改流程编号
     */
    @ApiOperation("修改流程编号")
    @PreAuthorize("@ss.hasPermi('masterData:change:edit')")
    @PutMapping("/change")
    public AjaxResult editByNo(@RequestBody Change change)
    {
        change.setUpdateBy(getUsername());
        return toAjax(changeService.updateChange(change));
    }

    /**
     * 删除变更申请
     */
    @ApiOperation("删除变更申请")
    @PreAuthorize("@ss.hasPermi('masterData:change:remove')")
	@DeleteMapping("/{changeIds}")
    public AjaxResult remove(@PathVariable Long[] changeIds)
    {
        return toAjax(changeService.deleteChangeByChangeIds(changeIds));
    }

    /**
     * 获取编码
     */
    @ApiOperation("获取编码")
    @PreAuthorize("@ss.hasPermi('masterData:change:add')")
    @GetMapping("/getCode")
    public AjaxResult getcode(){
        return success(changeService.getCode());
    }
}
